import util from '@/libs/util';
import getDate from '@/util/getDate.js';
import vSwitch from '@/util/switch.vue';
import customTree from '../studentsignup/customTree.vue';

import Cookies from 'js-cookie';

export default {
    components: {
        vSwitch, customTree
    },
    name: 'exportable-table',
    data () {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                align: 'center',
                width: 60,
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1));
                }
            }, {
                title: '教师姓名',
                key: 'teacher_name',
                render: (h, params) => {
                    return h('a', {
                        on: {
                            click: () => {
                                this.formData.teacher_id = params.row.teacher_id;
                                this.isRead = true;
                                this.showEdit();
                            }
                        }
                    }, params.row.teacher_name);
                }

            }, {
                title: '部门',
                key: 'department_name'
            }, {
                title: '班级名称',
                key: 'class_name'
            }, {
                title: '教室',
                key: 'class_room'
            }, {
                title: '上课时间',
                key: 'class_time',
                width: 300
            }, {
                title: '星期',
                key: 'classType'
            }, {
                title: '所属课次',
                key: 'class_no'
            }, {
                title: '状态',
                key: 'class_type',
                render: (h, params) => {
                    let text = '';
                    if (params.row.class_type === 1) text = '未上';
                    if (params.row.class_type === 2) text = '正常';
                    if (params.row.class_type === 3) text = '请假';
                    if (params.row.class_type === 4) text = '迟到';
                    return h('div', {}, text);
                }
            }],
            data: [],
            formData: {},
            query: {},
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            baseuser: JSON.parse(Cookies.get('baseUser')),
            modelVisible: false,
            sexList: [{id: 'MAN', name: '男'}, {id: 'WOMAN', name: '女'}],
            learnList: [],
            classList: [],
            teachertypeList: [],
            mainTeacherList: [],
            departmentList: [],
            positionList: [],
            viewModal: false,
            deptModal: false,
            editTitle: '',
            isRead: false
        };
    },
    mounted () {
        this.loadDictionaries();
        this.loadList();
    },
    methods: {
        loadList () {
            util.ajax.post('/busine/classes/schedule/detail/getTeacherScheduleList', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                status: this.query.status ? this.query.status : '',
                teacherName: this.query.teacherName ? this.query.teacherName : '',
                className: this.query.className ? this.query.className : '',
                departmentId: this.query.departmentId ? this.query.departmentId : ''
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        setDept () {
            if (this.$refs.dept.selectData[0]) {
                this.query.departmentId = this.$refs.dept.selectData[0].id;
                this.query.departmentName = this.$refs.dept.selectData[0].title;
            } else {
                this.query.departmentId = '';
                this.query.departmentName = '';
            }
            this.deptModal = false;
        },
        cancelSelect () {
            this.$refs.dept.cancelSelect();
        },
        loadDictionaries () {
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'LEARN_EXP'
            })).then(res => {
                this.learnList = res.data.data;
            });
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'TEACHER_TYPE'
            })).then(res => {
                this.teachertypeList = res.data.data;
            });
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'POSITION_TYPE'
            })).then(res => {
                this.positionList = res.data.data;
            });
        },
        refresh () {
            this.query = {};
            this.loadList();
        },
        showEdit (obj) {
            this.editTitle = '教师信息';
            util.ajax.post('/busine/teacher/detail', util.toFormData({
                id: this.formData.teacher_id
            })).then(res => {
                if (res.data.data) {
                    this.formData = res.data.data;
                    this.formData.birthday = util.getDate(this.formData.birthday, 'date');
                    this.formData.teachtime = util.getDate(this.formData.teachtime, 'date');
                    this.formData.orgname = this.formData.organize ? this.formData.organize.name : '';
                    this.formData.majorsname = this.formData.majorsName;
                    this.ids = this.formData.majorIds.split(',');
                    this.viewModal = true;
                    this.isRead = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        }
    }
}